﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;


namespace SocketClient
{
    public partial class LoginForm : Form
    {
        //OutputMsg outMsg = new OutputMsg();
        public string domainid = "...";
        public bool enablelogin = false;
        // 定义委托
        public delegate void TransferValueEventHandler(string value);

        // 定义事件
        public event TransferValueEventHandler TransferValue;
        public LoginForm()
        {
            InitializeComponent();
            NetApi netapi = new NetApi();
            netapi.InitDomainConfig();
            netapi.GetDomainTenant();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string postData = "{\"username\":\"" + textBox1.Text.Trim() + "\",\"password\":\"" + textBox2.Text.Trim() + "\",\"captchaFlag\":\"true\"}";
            Http.CreateHttpClient(GValues.domainLink + "sys/login", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<LoginDTO> responResult = JsonConvert.DeserializeObject<ResponResult<LoginDTO>>(r.AckStr());
                    if (responResult.success)
                    {
                        GValues.account = textBox1.Text;
                        GValues.password = textBox2.Text ; ;
                        GValues.token = responResult.result.token;
                        SqlLiteUtils sqlLite = new SqlLiteUtils();
                        sqlLite.createDb();
                        sqlLite.insertData(GValues.account, StringSecurity.DESEncrypt(textBox2.Text), GValues.tenant, GValues.token);
                        GValues.globalUrl = GValues.domainLink.Replace("http", "ws").Replace("https", "wss") + "websocket/" + GValues.tenant + "-" + GValues.account + "-print";
                        this.Close();
                    }
                    else
                    {
                         LogHelper.WriteLog("登入系统错误!" + responResult.code + " " + responResult.message,null);
                    }
                }
                else
                {
                     LogHelper.WriteLog("登入系统错误!" + r.Code + " " + " " + responCode + " " + r.Error,null);
                }
            }));
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
      
    }
}
